<?php
namespace App\Services;
use App\Services\OSS;
use App\Models\AreaModel;

class CommonService{
	/**
 	 * 随机生成token字符串
	 * @param
	 * @return string $_token
	 */
	public function getRandomToken(){
	    $token_list = array(
		    'JNNpvYuqg94oi0CvkuBIlyzbgD7QnklF4PDfBSTZ',
            'AB3Kwb3y7OTvnHwqDCiJ1Zy8LsrW4LBDO7licG4G',
            'TA1evI8IJ76ISDb5Gh2zHRqWlhvEc8qTXAYOQqY1',
            'PjhiaCTXi3J7fEmRjQeYLGo144Opgflzru4k1wYA',
            'gpJ5WO87AEKqgJ0Pi7hMkksM5OOlAhIMFOo2YwXe',
            '2eclqjnogiY9HYlOSvrWBQVZWg0yaiuE30AwFviF',
            'fj2vhLQK2L3YFi1u9PYXx8mXBzTr9WOtmcWZ97Xz',
            'TDpyUWEhKB1n9mvpjme5MTg9n7mt8PgeJLWItEFw',
            'N3H19XgcRnVSqCTvSCXiZIVzKUZjy6GDyCD1Tbge',
            'XiXGWEIvewr47YT7MPtYtSiCfQJHUjGt8e4iSt07',
            'AphlB7hnOQ7t6CKpL6t0p5HKWifrNfXJEyruOfIt',
            'kPiyQFJYqGRr0V5bjlt4uWaOIG7Bq11kp84NTnGd',
            'GVQZQcK1wjqKgIc31PO1DspkX9XTAigVFqjMvlA5',
            'rGDEPFyE49c3Y1DY5hKuGubyqmo5wNlgXflgLZRg',
            '4DtpQSOv6dWalaOsBjCF1O27hDh6bx7oDHg0lI2X',
            'nmlqiiW1z6qGyZCgfeIrGPfVCx5J0yhXRqFswzJr',
            'pcHrmW1Ea8SE24Fd85KB0mu8oiC9YbKBgGAXh4hm'
	    );

	    $token = array_rand($token_list);
	    $_token = $token_list[$token];
	    return $_token;
	}


	/**
	 * 随机生成6位a-zA-Z0-9字符串
	 * @param int $len
	 * @param string $chars
	 * @return string $str
	 */
	public function getRandomString($len, $chars=null){  
	    if (is_null($chars)) {  
	        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
	    }  
	    mt_srand(10000000*(double)microtime());  
	    for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++) {  
	        $str .= $chars[mt_rand(0, $lc)];  
	    }  
	    return $str;  
	}  

	/**
	 * 上传文件方法
	 * @param object $_file
	 * @param string $file_path
	 * @param string $_course_id
	 * @return array $data
	 */
	public function uploads($_file,$file_path,$_course_id = ''){
		if(!$_file->isValid()){
			$data = array('code'=>400);
		}else{
			// 获取扩展名
			$ext = $_file->getClientOriginalExtension();

			// 生成新文件名
			$newfile = md5(time().rand(1000,9999)).'.'.$ext;

			// 文件上传指定路径
			$path = public_path($file_path);

			if($_course_id > 0){
				$target_file = $file_path.'_'.$_course_id.'/'.$newfile;
			}else{
				$target_file = $file_path.'/'.$newfile;
			}

			$res = OSS::upload($target_file,$_file->getRealPath());

			if($res){
				$data = array('code'=>200,'imgurl' => $target_file);
			}else{
				$data = array('code'=>400);
			}
		}

		return $data;
	}

	/**
	 * 获取省份列表
	 * @param 
	 * @return array $provincelist
	 */
	public function getProvinceList(){
		$provincelist = AreaModel::where('pid',0)->select('id','name')->get()->toArray();
		return $provincelist;
	}

	/**
	 * 获取城市列表
	 * @param int $id 省份id
	 * @return array $citylist
	 */
	public function getCityList($id){
		$citylist = AreaModel::where('pid',$id)->select('id','name')->get()->toArray();
		return $citylist;
	}

	/**
	 * 获取城市信息
	 * @param string $name
	 * @return object $pcinfo
	 */
	public function getPcInfoByName($name){
		$pcinfo = AreaModel::where('name','like', '%'.$name.'%')->select('id','name','firstchar','pinyin','jianpin')->first()->toArray();
		return $pcinfo;
	}

	/**
	 * 获取城市信息
	 * @param int $id
	 * @return object $pcinfo
	 */
	public function getPcInfoById($id){
		$pcinfo = AreaModel::where('id',$id)->select('id','pid','name','firstchar','pinyin','jianpin')->first();
		return $pcinfo;
	}

	/**
	 * 获取城市列表
	 * @param int $level
	 * @return object $list
	 */
	public function getCityListByLevel($level){
		$citylist = AreaModel::orderBy('firstchar','asc')->where('level',2)->select('id','name','firstchar','pinyin','jianpin')->get()->toArray();

		$list = array();
		foreach($citylist as $ctm=>$cst){
			$list[$cst['firstchar']][] = $cst;
		}

		return $list;
	}
}
